import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';
// import 'dart:async';

// import 'package:webview_flutter/webview_flutter.dart';

class JYZPayPage extends StatefulWidget {
  String url;
  JYZPayPage({Key key, this.url}) : super(key: key);
  @override
  _JYZPayPageState createState() => _JYZPayPageState(url);
}

class _JYZPayPageState extends State<JYZPayPage> {
  String url;
  _JYZPayPageState(this.url);
  WebViewController _controller;
  FlutterWebviewPlugin plugin = new FlutterWebviewPlugin();
  @override
  Widget build(BuildContext context) {
    // TODO: im,plement build
    return 
    Platform.isIOS
        ? Scaffold(
            appBar: AppBar(
                backgroundColor: mainColor,
                title: Text(!widget.url.contains("czb365") ? '熊猫卡券' : '订单支付'),
                centerTitle: true,
                actions: <Widget>[
                  // new IconButton(
                  //     icon: new Icon(Icons.arrow_left),
                  //     onPressed: () {
                  //       _controller.goBack();
                  //     }),
                  new IconButton(
                      icon: new Icon(Icons.home),
                      onPressed: () {
                        // _controller.loadUrl(url);
                      })
                ]),
            body: WebView(
              initialUrl: url,
              javascriptMode: JavascriptMode.unrestricted,
              navigationDelegate: (NavigationRequest request) {
                print(request.url);
                if (request.url.startsWith('alipays://') ||
                    request.url.startsWith('alipay://')) {
                  //   print('blocking navigation to $request}');
                  _openApp(request.url);
                  // _controller.goBack();
                  return NavigationDecision.prevent;
                }
                if (request.url.startsWith('weixin://')) {
                  //   print('blocking navigation to $request}');
                  _openApp(request.url);
                  return NavigationDecision.prevent;
                }
                // print('allowing navigation to $request');
                return NavigationDecision.navigate;
              },
              onWebViewCreated: (WebViewController controller) {
                _controller = controller;
                controller.canGoBack().then((res) {});
                controller.currentUrl().then((url) {});
                controller.canGoForward().then((res) {});
              },
              onPageFinished: (String value) {
                // webview 页面加载调用
                // if (_controller != null) {
                //   _controller.currentUrl().then((url) {
                //     map['Referer'] = url;
                //      _controller.loadUrl(url, headers: map);
                //   });
                // }
              },
            ),
          )
        : 
    WebviewScaffold(
            url: url,
            appBar: AppBar(
                backgroundColor: mainColor,
                title: Text(!widget.url.contains("czb365") ? '熊猫卡券' : '订单支付'),
                centerTitle: true,
                actions: <Widget>[
                  // new IconButton(
                  //     icon: new Icon(Icons.arrow_left),
                  //     onPressed: () {
                  //       _controller.goBack();
                  //     }),
                  new IconButton(
                      icon: new Icon(Icons.home),
                      onPressed: () {
                      })
                ]),
          );
  }
  // @override
  // Widget build(BuildContext context) {
  //   return ;
  // }

  @override
  void initState() {
    super.initState();
    plugin.onUrlChanged.listen((String value) {

      if (value.startsWith('alipays://') || value.startsWith('alipay://')) {
        //   print('blocking navigation to $request}');
        _openApp(value);
        // 
        setState(() {
          // plugin.goBack();
          plugin.stopLoading();
          plugin.reload();
        });
      }
      if (value.startsWith('weixin://')) {
        //   print('blocking navigation to $request}');
        _openApp(value);
        setState(() {
          plugin.goBack();
        });
      }
    });
  }

  void _openApp(openurl) async {
    // Android
    print("open app");
    final url = 'vnd.' + openurl;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      //  Ios
      final url = openurl;
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        Fluttertoast.showToast(msg: "打开失败");
        // throw 'Could not launch $url';
      }
    }
  }

  void dispose() {
    super.dispose();
  }
}
